Skip to content

Add strategy threshold override API with OpenAI suggestion engine#987

Open
carkod wants to merge 3 commits intomasterfrom
codex/create-pr-using-codex-cloud
Open

Add strategy threshold override API with OpenAI suggestion engine#987
carkod wants to merge 3 commits intomasterfrom
codex/create-pr-using-codex-cloud

Conversation

@carkod
Copy link
Copy Markdown
Owner

@carkod carkod commented Apr 26, 2026

Motivation

  • Provide a system to recommend, apply, and store temporary strategy threshold overrides and allow automated recommendations from an LLM with guardrails to protect against risky market states.

Description

  • Create the strategy_threshold_override DB table and Alembic migration f7b8c9d0e1f2 with indexes and a full downgrade path.
  • Add StrategyThresholdOverrideTable SQLModel definition and wire it into databases.tables, plus a StrategyThresholdOverrideCrud class for create/get/list operations.
  • Implement API endpoints in strategy_thresholds.routes (/apply, /suggest, /active, list) and register the router as strategy_threshold_blueprint in main.
  • Integrate an OpenAI client via OpenAiThresholdAdvisor, add Pydantic models and guardrails in strategy_thresholds.models, and add config entries and dependency (openai in pyproject.toml) to support LLM-based suggestions.

Testing

  • Ran pytest api/tests/test_strategy_threshold_overrides.py, which covers applying an override, suggesting with mocked OpenAI (and applying), and rejecting recommendations when guardrails fail, and the tests passed.

Codex Task

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: adbbd17c6d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

session: Session = Depends(get_session),
_: dict = Depends(get_current_user),
):
expires_at = payload.market_context_timestamp + payload.recommendation.ttl_minutes * 60
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Convert TTL minutes to milliseconds for expiry timestamps

pybinbot.timestamp() is millisecond-based, but this computes expires_at with ttl_minutes * 60 (seconds). As a result, a 60-minute override only lasts about 3.6 seconds when market_context_timestamp is in ms, and the same unit mismatch is repeated in /suggest (expires_at=timestamp() + recommendation.ttl_minutes * 60), so get_active can treat valid overrides as expired almost immediately.

Useful? React with 👍 / 👎.

session: Session = Depends(get_session),
_: dict = Depends(get_current_user),
):
client = OpenAiThresholdAdvisor(model=payload.model)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle advisor initialization errors in suggest endpoint

OpenAiThresholdAdvisor is instantiated before the try block, but its constructor can raise OpenAiThresholdError (for example when OPENAI_API_KEY is unset). In that case /suggest returns an unhandled 500 instead of the intended structured rejection response (error=1), so misconfigured environments fail hard on this route.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant